package com.ma.home;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.Toast;

import com.ma.R;

import java.util.Date;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import base.BaseActivity;
import bean.LoginBean;
import bean.SycnRequstBean;
import database.dao.DBManager;
import database.table.*;
import httpUtil.Api;
import httpUtil.Http;
import utils.ConstantUtil;
import utils.JsonUtil;
import utils.StringUtil;

public class SycnActivity extends BaseActivity   {

    private LoginBean loginBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loginBean = (LoginBean) getIntent().getSerializableExtra(ConstantUtil.DATA1);
        setContentView(R.layout.activity_sycn);

        state.addObserver(new Observer() {
            @Override
            public void update(Observable observable, Object o) {
                State b = (State) o;

                if (b.flag_down_khda == -1 || b.flag_down_zyda == -1 || b.flag_down_bsgz == -1 || b.flag_down_jdb == -1 || b.flag_down_dbb == -1 || b.flag_down_wgb == -1 || b.flag_down_hfb == -1 || b.flag_down_gdb == -1 || b.flag_down_zys == -1) {


                } else if (b.sum()==9) {
                    logJson(b);

                     closeProgressDialog();

                }
            }
        });
        initData();


    }

        SycnRequstBean getRequstBean() {
        Date date = StringUtil.getDate("2016-11-29 18:50:20");
        int page = 0;
        int xmbs = loginBean.getBMJGData().get(0).getXMBS();
        SycnRequstBean bean = new SycnRequstBean();
        bean.setGXSJ(date);
        bean.setPageindex(page);
        bean.setXMBS(233);
        bean.setYH_ZJ(loginBean.getID());
        return bean;
    }

    public void initData() {
        showProgressDialog();
        getKHDA(getRequstBean());
        getZyda(getRequstBean());
        getZys(getRequstBean());
        getBsgz(getRequstBean());
        getJdb(getRequstBean());
        getDbb(getRequstBean());
        getGdb(getRequstBean());
        getWgb(getRequstBean());
        getHfb(getRequstBean());

    }

    State state = new State();


    public void getKHDA(SycnRequstBean bean) {
        bean.setPageindex(bean.getPageindex() + 1);
        Http.sycn(Api.GetKHDA, bean)
                .subscribe(res -> {
                    List<Khda> list = JsonUtil.getList(res.getContent(), Khda.class);
                    if (!list.isEmpty()) {
                        DBManager.get().getKhdaDao().insertOrReplaceInTx(list);
                        if (list.size() == 10)
                            getKHDA(bean);
                        else {

                            state.updateFlag_down_khda(1);
                        }
                    } else {
                        state.updateFlag_down_khda(1);

                    }

                }, throwable -> {
                    state.updateFlag_down_khda(-1);

                });

    }

    public void getZyda(SycnRequstBean bean) {
        bean.setPageindex(bean.getPageindex() + 1);
        Http.sycn(Api.GetZYDA, bean)
                .subscribe(res -> {
                            List<Zyda> list = JsonUtil.getList(res.getContent(), Zyda.class);
                            if (!list.isEmpty()) {
                                DBManager.get().getZydaDao().insertOrReplaceInTx(list);
                                if (list.size() == 10) getZyda(bean);
                                else {


                                    state.updateFlag_down_zyda(1);
                                }
                            } else {

                                state.updateFlag_down_zyda(1);
                            }
                        }, throwable -> {
                            state.updateFlag_down_zyda(-1);

                        }

                );

    }

    public void getZys(SycnRequstBean bean) {
        bean.setPageindex(bean.getPageindex() + 1);
        Http.sycn(Api.GetZYS, bean)
                .subscribe(res -> {
                            List<Zys> list = JsonUtil.getList(res.getContent(), Zys.class);
                            if (!list.isEmpty()) {
                                DBManager.get().getZysDao().insertOrReplaceInTx(list);
                                if (list.size() == 10) getZys(bean);
                                else {

                                    state.updateFlag_down_zys(1);
                                }
                            } else {

                                state.updateFlag_down_zys(1);
                            }
                        }, throwable -> {
                            state.updateFlag_down_zys(-1);

                        }

                );

    }

    public void getBsgz(SycnRequstBean bean) {
        bean.setPageindex(bean.getPageindex() + 1);
        Http.sycn(Api.GetBSGZ, bean)
                .subscribe(res -> {
                            List<Bsgz> list = JsonUtil.getList(res.getContent(), Bsgz.class);
                            if (!list.isEmpty()) {
                                DBManager.get().getBsgzDao().insertOrReplaceInTx(list);
                                if (list.size() == 10) getBsgz(bean);
                                else {

                                    state.updateFlag_down_bsgz(1);
                                }
                            } else {

                                state.updateFlag_down_bsgz(1);
                            }
                        }, throwable -> {
                            state.updateFlag_down_bsgz(-1);

                        }

                );

    }

    public void getJdb(SycnRequstBean bean) {
        bean.setPageindex(bean.getPageindex() + 1);
        Http.sycn(Api.GetJDB, bean)
                .subscribe(res -> {
                            List<Jdb> list = JsonUtil.getList(res.getContent(), Jdb.class);
                            if (!list.isEmpty()) {
                                DBManager.get().getJdbDao().insertOrReplaceInTx(list);
                                if (list.size() == 10) getJdb(bean);
                                else {

                                    state.updateFlag_down_jdb(1);
                                }
                            } else {

                                state.updateFlag_down_jdb(1);
                            }
                        }, throwable -> {
                            state.updateFlag_down_jdb(-1);

                        }

                );

    }

    public void getDbb(SycnRequstBean bean) {
        bean.setPageindex(bean.getPageindex() + 1);
        Http.sycn(Api.GetDBB, bean)
                .subscribe(res -> {
                            List<Dbb> list = JsonUtil.getList(res.getContent(), Dbb.class);
                            if (!list.isEmpty()) {
                                DBManager.get().getDbbDao().insertOrReplaceInTx(list);
                                if (list.size() == 10) getDbb(bean);
                                else {

                                    state.updateFlag_down_dbb(1);
                                }
                            } else {

                                state.updateFlag_down_dbb(1);
                            }
                        }, throwable -> {
                            state.updateFlag_down_dbb(-1);

                        }

                );

    }

    public void getGdb(SycnRequstBean bean) {
        bean.setPageindex(bean.getPageindex() + 1);
        Http.sycn(Api.GetGDB, bean)
                .subscribe(res -> {
                            List<Gdb> list = JsonUtil.getList(res.getContent(), Gdb.class);
                            if (!list.isEmpty()) {
                                DBManager.get().getGdbDao().insertOrReplaceInTx(list);
                                if (list.size() == 10) getGdb(bean);
                                else {

                                    state.updateFlag_down_gdb(1);
                                }
                            } else {

                                state.updateFlag_down_gdb(1);
                            }
                        }, throwable -> {
                            state.updateFlag_down_gdb(-1);

                        }

                );

    }

    public void getWgb(SycnRequstBean bean) {
        bean.setPageindex(bean.getPageindex() + 1);
        Http.sycn(Api.GetWGB, bean)
                .subscribe(res -> {
                            List<Wgb> list = JsonUtil.getList(res.getContent(), Wgb.class);
                            if (!list.isEmpty()) {
                                DBManager.get().getWgbDao().insertOrReplaceInTx(list);
                                if (list.size() == 10) getWgb(bean);
                                else {

                                    state.updateFlag_down_wgb(1);
                                }
                            } else {

                                state.updateFlag_down_wgb(1);
                            }
                        }, throwable -> {
                            state.updateFlag_down_wgb(-1);

                        }

                );

    }

    public void getHfb(SycnRequstBean bean) {
        bean.setPageindex(bean.getPageindex() + 1);
        Http.sycn(Api.GetHFB, bean)
                .subscribe(res -> {
                            List<Hfb> list = JsonUtil.getList(res.getContent(), Hfb.class);
                            if (!list.isEmpty()) {
                                DBManager.get().getHfbDao().insertOrReplaceInTx(list);
                                if (list.size() == 10) getHfb(bean);
                                else {
                                    state.updateFlag_down_hfb(1);
                                }
                            } else {

                                state.updateFlag_down_hfb(1);
                            }
                        }, throwable -> {
                            state.updateFlag_down_hfb(-1);

                        }

                );

    }




    public class State extends Observable {


        public int flag_down_khda;
        public int flag_down_zyda;
        public int flag_down_bsgz;
        public int flag_down_jdb;
        public int flag_down_dbb;
        public int flag_down_wgb;
        public int flag_down_hfb;
        public int flag_down_gdb;
        public int flag_down_zys;

        public int getFlag_down_zys() {
            return flag_down_zys;
        }

        public void updateFlag_down_zys(int flag_down_zys) {
            if (this.flag_down_zys == flag_down_zys) return;
            this.flag_down_zys = flag_down_zys;
            setChanged();
            notifyObservers(this);
        }


        public int getFlag_down_khda() {
            return flag_down_khda;
        }

        public void updateFlag_down_khda(int flag_down_khda) {
            if (this.flag_down_khda == flag_down_khda) return;
            this.flag_down_khda = flag_down_khda;
            setChanged();
            notifyObservers(this);
        }

        public int getFlag_down_zyda() {
            return flag_down_zyda;
        }

        public void updateFlag_down_zyda(int flag_down_zyda) {
            if (this.flag_down_zyda == flag_down_zyda) return;
            this.flag_down_zyda = flag_down_zyda;
            setChanged();
            notifyObservers(this);
        }

        public int getFlag_down_bsgz() {
            return flag_down_bsgz;
        }

        public void updateFlag_down_bsgz(int flag_down_bsgz) {
            if (this.flag_down_bsgz == flag_down_bsgz) return;
            this.flag_down_bsgz = flag_down_bsgz;
            setChanged();
            notifyObservers(this);
        }

        public int getFlag_down_jdb() {
            return flag_down_jdb;
        }

        public void updateFlag_down_jdb(int flag_down_jdb) {
            if (this.flag_down_jdb == flag_down_jdb) return;
            this.flag_down_jdb = flag_down_jdb;
            setChanged();
            notifyObservers(this);
        }

        public int getFlag_down_dbb() {
            return flag_down_dbb;
        }

        public void updateFlag_down_dbb(int flag_down_dbb) {
            if (this.flag_down_dbb == flag_down_dbb) return;
            this.flag_down_dbb = flag_down_dbb;
            setChanged();
            notifyObservers(this);
        }

        public int getFlag_down_wgb() {
            return flag_down_wgb;
        }

        public void updateFlag_down_wgb(int flag_down_wgb) {
            if (this.flag_down_wgb == flag_down_wgb) return;
            this.flag_down_wgb = flag_down_wgb;
            setChanged();
            notifyObservers(this);
        }

        public int getFlag_down_hfb() {
            return flag_down_hfb;
        }

        public void updateFlag_down_hfb(int flag_down_hfb) {
            if (this.flag_down_hfb == flag_down_hfb) return;
            this.flag_down_hfb = flag_down_hfb;
            setChanged();
            notifyObservers(this);
        }

        public int getFlag_down_gdb() {
            return flag_down_gdb;
        }

        public void updateFlag_down_gdb(int flag_down_gdb) {
            if (this.flag_down_gdb == flag_down_gdb) return;
            this.flag_down_gdb = flag_down_gdb;
            setChanged();
            notifyObservers(this);
        }

        public  int sum(){

            return  flag_down_khda + flag_down_zyda + flag_down_bsgz + flag_down_jdb + flag_down_dbb + flag_down_wgb + flag_down_hfb + flag_down_gdb + flag_down_zys;
        }
    }


}
